package com.example.likestarlibrary;

import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.*;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

/**
 * 飘心动画心形界面类
 * 一个TCHeartView代表一个心形，由heart.png组成
 */
public class TCHeartView extends Image {

    private Paint sPaint = new Paint();
    private Canvas sCanvas = new Canvas();
    private  PixelMap sHeart;
    private  PixelMap sHeartBorder;
    private int mHeartResId = ResourceTable.Media_heart1;
    private int mHeartBorderResId = ResourceTable.Media_heart1;

    public TCHeartView(Context context) {
        super(context);
        sPaint.setAntiAlias(true);
        sPaint.setFilterBitmap(true);
    }

    private static PixelMap createBitmapSafely(int width, int height) {
        try {
            PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
            opts.size = new Size(width, height);
            opts.pixelFormat = PixelFormat.ARGB_8888;
            return PixelMap.create(opts);
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
        }
        return null;
    }

    public void setDrawable(PixelMapElement bitmap) {
        setImageElement(bitmap);
    }

    public void setColor(int color) {
        try {
            PixelMap heart = createHeart(color);
            setImageElement(new PixelMapElement(heart));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setColorAndDrawables(int color, int heartResId, int heartBorderResId) {
        if (heartResId != mHeartResId) {
            sHeart = null;
        }
        if (heartBorderResId != mHeartBorderResId) {
            sHeartBorder = null;
        }
        mHeartResId = heartResId;
        mHeartBorderResId = heartBorderResId;
        setColor(color);
    }

    private PixelMap createHeart(int color) throws Exception {
        if (sHeart == null) {
            sHeart = ImageSource.create(getResourceManager().getResource(mHeartResId), new ImageSource.SourceOptions()).createPixelmap(null);
        }
        if (sHeartBorder == null) {
            sHeartBorder = ImageSource.create(getResourceManager().getResource(mHeartBorderResId), new ImageSource.SourceOptions()).createPixelmap(null);
        }
        PixelMap heart = sHeart;
        PixelMap heartBorder = sHeartBorder;
        Size hSize = heart.getImageInfo().size;
        Size hbSize = heartBorder.getImageInfo().size;
        PixelMap bm = createBitmapSafely(hbSize.width, hbSize.height);
        if (bm == null) {
            return null;
        }
        Canvas canvas = sCanvas;
        canvas.setTexture(new Texture(bm));
        Paint p = sPaint;
        canvas.drawPixelMapHolder(new PixelMapHolder(heartBorder), 0, 0, p);
        p.setColorFilter(new ColorFilter(color, BlendMode.SRC_ATOP));
        float dx = (hbSize.width - hSize.width) / 2f;
        float dy = (hbSize.height - hSize.height) / 2f;
        canvas.drawPixelMapHolder(new PixelMapHolder(heart), dx, dy, p);
        p.setColorFilter(null);
        canvas.setTexture(null);
        return bm;
    }

}
